/**
 * 时间转换工具
 * timestamp: 时间撮
 * format 需要输入的格式，例如： Y-M-D h:m:s W
 */
export const formatTime = (timestamp, format) => {
	const formateArr = ['Y', 'M', 'D', 'h', 'm', 's', 'W'];
	let returnArr = [];

	let tStamp;
	let date;

	timestamp = timestamp+''
	
	if (timestamp != 'now') {
		if (timestamp.indexOf("-") != -1) {
			timestamp=timestamp.replace(/\-/g, '/');
			tStamp = new Date(timestamp).getTime()
		} else {
			tStamp = timestamp*1000
		}
		
		date = new Date(tStamp);
		
	} else {
		date = new Date();
	}



	let year = date.getFullYear()
	let month = date.getMonth() + 1
	let day = date.getDate()
	let hour = date.getHours()
	let minute = date.getMinutes()
	let second = date.getSeconds()

	let week = date.getDay()
	switch (week) {
		case 0:
			week = '星期日';
			break;
		case 1:
			week = '星期一';
			break;
		case 2:
			week = '星期二';
			break;
		case 3:
			week = '星期三';
			break;
		case 4:
			week = '星期四';
			break;
		case 5:
			week = '星期五';
			break;
		case 6:
			week = '星期六'
	}

	returnArr.push(year, month, day, hour, minute, second, week);

	returnArr = returnArr.map(formatNumber);

	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	return format;
}
const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}


/*
 *登录校验
 */
export const checkAuthority = () => {

	const token = uni.getStorageSync("TOKEN");
	if (!token) {
		uni.reLaunch({
			url: "/pages/login/index"
		})
		return false
	} else {
		return true
	}

}



/*
 *获取列表切换
 *node: 节点
 *direction: 滚动方向 x:横向 | y:纵向
 *lineAll: 整体横线的偏移
 *scrollLeft：屏幕滚动居中的位置。值越大越靠右
 *callback：回调函数
 */
export const getSetScroll = (node, direction, lineAll, scrollLeft, callback) => {
	let scrollWidth = 0,
		scrollList = [],
		offList = [],
		itemWidth = [];
	uni.createSelectorQuery().selectAll(node).boundingClientRect(function(rects) {
		rects.forEach(function(rect, index) {
			// 横向
			if (direction == 'x') {
				// 第一个处理
				if (scrollWidth == 0) {
					//计算scroll滑动的距离
					let sX = 0;
					scrollList.push(sX);
					//计算横线滑动的距离
					let oX = rect.left * 1 + rect.width * 1 / 2 - lineAll * 1;
				 offList.push(oX);
				} else {
					//计算scroll滑动的距离
					let sX = scrollWidth + rect.width / 2 - scrollLeft;
					scrollList.push(sX);
					//计算横线滑动的距离
					let oX = rect.left * 1 + rect.width * 1 / 2 - lineAll * 1;
					if (index > 3 && oX < offList[index - 1]) {
						console.log(rect.width * 1)
						oX = offList[index - 1] + rect.width * 1 + 8.5
					}
					offList.push(oX);
				}
				//计算scroll的宽度
				scrollWidth += rect.width * 1;
				itemWidth.push(rect.width * 1);
			}
			// 纵向
			else {
				// 第一个处理
				if (scrollWidth == 0) {
					//计算scroll滑动的距离
					let sX = 0;
					scrollList.push(sX);
					//计算横线滑动的距离
					let oX = rect.top * 1 + rect.top * 1 / 2 - lineAll * 1;
					offList.push(oX);
				} else {
					//计算scroll滑动的距离
					let sX = scrollWidth / 2 + rect.top / 2 - scrollLeft;
					scrollList.push(sX);
					//计算横线滑动的距离
					let oX = rect.top * 1 + rect.top * 1 / 2 - lineAll * 1;
					offList.push(oX);
				}
				//计算scroll的宽度
				scrollWidth += rect.height * 1;
				itemWidth.push(rect.height * 1);
			}
		})
		//结果组装
		let obj = {
			scrollWidth: scrollWidth,
			itemWidth: itemWidth,
			scrollList: scrollList,
			offList: offList,
		}
		callback(obj);
	}).exec();
}
/**
 * 动态获取滚动区域高度
 * obj: 顶部或底部的class类
 * offset: 偏移量，一般用于内外边距
 * callback: 回调方法
 */
export const getListHeight = (obj, offset, _this, callback) => {
	//传参校验
	if (!obj) {
		uni.showToast({
			title: 'getListHeight方法传参错误',
			icon: 'none'
		})
		console.log('getListHeight方法传参错误,obj：必填 | offset：必填  | callback：必填')
		return
	}
	uni.getSystemInfo({
		success: function(res) {
			let height = res.windowHeight;
			let query = uni.createSelectorQuery().select('.' + obj).boundingClientRect(function(rects) {
				if (offset) {
					obj = res.windowHeight * 1 - rects.height * 1 - offset * 1;
				} else {
					obj = res.windowHeight * 1 - rects.height * 1;
				}
				callback(obj)
			}).exec();
		}
	});
}
